package com.gitee.liuzhi.framework.starter.response.handler;


import com.gitee.liuzhi.framework.starter.response.apiresult.DataResult;
import com.gitee.liuzhi.framework.starter.response.enums.DataResultEnum;
import com.gitee.liuzhi.framework.starter.response.exception.BusinessException;
import com.gitee.liuzhi.framework.starter.response.exception.FileException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * Created by 2022/4/29/0029
 *
 * @author LiuZhi
 * @version V1.0
 * @description: 全局异常处理
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(BusinessException.class)
    public Object execute(BusinessException businessException) {
        return DataResult.result(DataResultEnum.BUSINESS_ERROR.getCode(), businessException.getMessage());
    }

    @ExceptionHandler(FileException.class)
    public Object execute(FileException fileException) {
        return DataResult.result(DataResultEnum.FILE_ERROR.getCode(), fileException.getMessage());
    }

    @ExceptionHandler(Exception.class)
    public Object execute(Exception exception) {
        log.error(String.format("exception. message : %s", exception.getMessage()), exception);
        return DataResult.result(DataResultEnum.INVOKE_FAILURE.getCode(), DataResultEnum.INVOKE_FAILURE.getMessage());
    }

}
